//
//  BinSpecTableview.swift
//  Wmsmobile
//
//  Created by Nick xu on 16/8/19.
//  Copyright © 2016年 www.hd123.com. All rights reserved.
//

import UIKit
fileprivate func < <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
  switch (lhs, rhs) {
  case let (l?, r?):
    return l < r
  case (nil, _?):
    return true
  default:
    return false
  }
}

fileprivate func > <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
  switch (lhs, rhs) {
  case let (l?, r?):
    return l > r
  default:
    return rhs < lhs
  }
}


class chkBinRtnArtTableView: UITableView, UITableViewDelegate, UITableViewDataSource ,DetailListCellDisplayViewProtcol {
    
    //MARK: - property
    var realHeightDelegate: DetailListCellProtcol?
    
    var heightDelegate: DetailListCellProtcol?{
        set{
            realHeightDelegate = newValue
        }
        get{
            return realHeightDelegate
        }
    }
    var anyInfo:AnyObject?{
        didSet{
            let dataArrayTemp = anyInfo as? [[String : String]]
            let ElementsCount = dataArrayTemp?.first?.keys.count
            var dataArrayTemp2 = [[String : String]]()
            for _ in 0..<Int(ElementsCount ?? 1) - 1{
                dataArrayTemp2.append([String : String]())
            }
            for item in dataArrayTemp! {
                let keyName = item["标题"]
                for n in 0..<Int(ElementsCount!) - 1 {
                    dataArrayTemp2[n].updateValue(item["值\(n)"]!, forKey: keyName!)
                }
            }
            dataArray = dataArrayTemp2
            
        }
    }
    var dataArray:[[String : String]]?{
        didSet{
            self.reloadData()
            if self.delegate != nil{
                self.heightDelegate?.setRealHeight(44 * CGFloat((dataArray?.count)!))
            }
        }
    }
    
    override init(frame: CGRect, style: UITableViewStyle) {
        super.init(frame: frame, style: style)
        self.register(chkBinRtnArtTableViewCell.self, forCellReuseIdentifier: "BinCell")
        self.delegate = self
        self.dataSource = self
        self.isScrollEnabled = false
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(frame: CGRect(x: 0, y: 0, width: 0, height: 0), style: UITableViewStyle.grouped)
        self.register(chkBinRtnArtTableViewCell.self, forCellReuseIdentifier: "BinCell")
        self.delegate = self
        self.dataSource = self
        self.isScrollEnabled = false
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArray?.count ?? 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "BinCell") as! chkBinRtnArtTableViewCell
        if self.dataArray?.count > 0{
            let info = self.dataArray![(indexPath as NSIndexPath).row]
            cell.info = info
        }
        return cell
    }
    
}
